﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.ResourceManage.Controllers
{

    /// <summary>
    /// 创建人：yzp
    /// 日 期：2018.04.25
    /// 描 述：楼盘信息
    /// </summary>
    public class ZG_PStructsController : MvcControllerBase
    {
        //
        // GET: /ResourceManage/ZG_PStructs/
        private OrganizeCache organizeCache = new OrganizeCache();
        private PStructCache pstructCache = new PStructCache();
        private PropertyBLL probll = new PropertyBLL();
        private AreaBLL areaBLL = new AreaBLL();

        #region 视图功能
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit)]  
        public ActionResult Index()
        {
            return View();
        }

        #endregion

        #region 查询数据
        /// <summary>
        /// 获取筛选树形
        /// </summary>
        /// <param name="queryJson">Type:类型（1-区域 2-机构） areaId：区域Id  Id:异步父级Id  IsSubstep:是否异步</param>
        /// <returns>List<TreeEntity></returns>
        [HttpPost]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "获取筛选树形", "GetPropertyQueryTreeJson")]
        public ActionResult GetPropertyQueryTreeJson(string queryJson)
        {
            var treeList = new List<TreeEntity>();
            if (!string.IsNullOrEmpty(queryJson))
            {
                #region 参数处理
                var queryParam = queryJson.ToJObject();
                string type = !queryParam["Type"].IsEmpty() ? queryParam["Type"].ToString() : "1";
                string areaId = !queryParam["AreaId"].IsEmpty() ? queryParam["AreaId"].ToString() : "310100";
                string id = !queryParam["Id"].IsEmpty() ? queryParam["Id"].ToString() : "";
                bool isAny = !queryParam["IsSubstep"].IsEmpty() ? queryParam["IsSubstep"].ToBool() : false;
                #endregion
                switch (type)
                {
                    #region 区域树
                    case "1":
                        var data1 = areaBLL.GetList(areaId).ToList();
                        foreach (AreaEntity item in data1)
                        {
                            TreeEntity tree = new TreeEntity();
                            tree.id = item.AreaId;
                            tree.text = item.AreaName;
                            tree.value = item.AreaId;
                            tree.title = item.AreaCode;
                            tree.Attribute = "LAT";
                            tree.AttributeValue = item.LAT == null ? "" : item.LAT.ToString();
                            tree.parentId = "0";
                            tree.AttributeA = "LON";
                            tree.AttributeValueA = item.LON == null ? "" : item.LON.ToString();
                            tree.isexpand = false;
                            tree.complete = true;
                            tree.hasChildren = false;
                            treeList.Add(tree);
                        }
                        break;
                    #endregion
                    #region 机构树
                    case "2":
                        var data = organizeCache.GetList().ToList();
                        foreach (OrganizeEntity item in data)
                        {
                            TreeEntity tree = new TreeEntity();
                            bool hasChildren = data.Count(t => t.ParentId == item.OrganizeId) == 0 ? false : true;
                            tree.id = item.OrganizeId;
                            tree.text = item.FullName;
                            tree.value = item.OrganizeId;
                            tree.isexpand = true;
                            tree.complete = true;
                            tree.hasChildren = hasChildren;
                            tree.img = "fa fa-sitemap";
                            if (!OperatorProvider.Provider.Current().IsSystem) tree.parentId = item.OrganizeId == OperatorProvider.Provider.Current().CompanyId ? "0" : item.ParentId;
                            else tree.parentId = item.ParentId;
                            treeList.Add(tree);
                        }
                        break;
                    #endregion
                    default: break;
                }
            }
            return Content(treeList.TreeToJson());
        }

        /// <summary>
        /// 分页获取楼盘数据列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">筛选条件---机构:OrganizeId 经营类型:BussinessScope 项目状态:PropertyState 区域:Region 项目类型:ProjectType 物业类型:ProjectCategory 模糊查询 （项目名称、编码、地址）:KeyWord}</param>
        /// <returns> rows=data{  主键：Id, 项目名称：Name, 项目编号：Code,  区域：Region,  地址：Address, 项目类型：ProjectType，  组织机构：OrganizeId， 面积：Area，  房间数：Count，  描述：Memo  },records:查询记录数,conditionJson：查询总面积, costtime：查询耗时, total：查询总页数, page：当期页码</returns>
        ///  total = new { records = pagination.records(查询数量), conditionJson = pagination.conditionJson（总面积） };
        [HttpPost]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "分页获取楼盘数据列表", "GetPropertyPageList")]
        public ActionResult GetPropertyPageList(Pagination pagination, string queryJson)
        {

            var queryParam = queryJson.ToJObject();
            if (!string.IsNullOrEmpty(queryJson))
            {
                if (!queryParam["OrganizeId"].IsEmpty()) queryParam["OrganizeId"] = organizeCache.GetSonOrganizeId(queryParam["OrganizeId"].ToString());
                queryJson = queryParam.ToJson();
            }
            var Regiondata = areaBLL.GetList("310100").ToList();
            var Organizedata = organizeCache.GetList();
            var data = probll.GetPageList(pagination, queryJson);
            var pstructdata = pstructCache.GetList().Where(e => e.Type == 1);
            var Room = new RoomCache().GetList().Where(e => e.SplitedMark == 0 || e.SplitedMark == 1 || e.SplitedMark == 3);
            var PRoom = pstructCache.GetList().Where(e => e.Type == 5);
            var countdata = Room.Join(PRoom, x => x.PStructId, y => y.Id, (x, y) => new { AllCode = y.AllCode });
            var Data = data.Join(pstructdata, x => x.PStructId, y => y.Id, (x, y) => new
            {
                Id = x.Id,
                PStructId = x.PStructId,
                Name = y.Name,
                Code = y.Code,
                Region = Regiondata.FirstOrDefault(e => e.AreaId == x.Region).IsEmpty() ? x.Region : Regiondata.FirstOrDefault(e => e.AreaId == x.Region).AreaName,
                Address = x.Address,
                ProjectCategory = x.ProjectCategory,
                OrganizeId = Organizedata.FirstOrDefault(e => e.OrganizeId == x.OrganizeId).IsEmpty() ? x.OrganizeId : Organizedata.FirstOrDefault(e => e.OrganizeId == x.OrganizeId).FullName,
                Area = x.Area,
                State = x.State,
                Count = countdata.Count(e => e.AllCode.Contains(y.Id)),
                Memo = x.Memo
            });
            var Total = new { records = pagination.records, conditionJson = pagination.conditionJson };
            var JsonData = new
            {
                rows = Data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = Total.ToJson()
            };
            return Content(JsonData.ToJson());
        }

        #endregion

        #region 保存数据

        #endregion


    }
}
